home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / os2 / e33el2.zip / emacs / 19.33 / lisp / cal-coptic.el < prev    next >
Lisp/Scheme  |  1996-02-27  |  10KB  |  235 lines

  1. ;;; cal-coptic.el --- calendar functions for the Coptic/Ethiopic calendars.
  2.  
  3. ;; Copyright (C) 1995 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
  6. ;; Keywords: calendar
  7. ;; Human-Keywords: Coptic calendar, Ethiopic calendar, calendar, diary
  8.  
  9. ;; This file is part of GNU Emacs.
  10.  
  11. ;; GNU Emacs is free software; you can redistribute it and/or modify
  12. ;; it under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation; either version 2, or (at your option)
  14. ;; any later version.
  15.  
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. ;; GNU General Public License for more details.
  20.  
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs; see the file COPYING.  If not, write to the
  23. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  24. ;; Boston, MA 02111-1307, USA.
  25.  
  26. ;;; Commentary:
  27.  
  28. ;; This collection of functions implements the features of calendar.el and
  29. ;; diary.el that deal with the Coptic and Ethiopic calendars.
  30.  
  31. ;; Comments, corrections, and improvements should be sent to
  32. ;;  Edward M. Reingold               Department of Computer Science
  33. ;;  (217) 333-6733                   University of Illinois at Urbana-Champaign
  34. ;;  reingold@cs.uiuc.edu             1304 West Springfield Avenue
  35. ;;                                   Urbana, Illinois 61801
  36.  
  37. ;;; Code:
  38.  
  39. (require 'cal-julian)
  40.  
  41. (defvar coptic-calendar-month-name-array
  42.   ["Tut" "Babah" "Hatur" "Kiyahk" "Tubah" "Amshir" "Baramhat" "Barmundah"
  43.    "Bashans" "Baunah" "Abib" "Misra" "al-Nasi"])
  44.  
  45. (defvar coptic-calendar-epoch (calendar-absolute-from-julian '(8 29 284))
  46.   "Absolute date of start of Coptic calendar = August 29, 284 A.D. (Julian).")
  47.  
  48. (defconst coptic-name "Coptic")
  49.  
  50. (defun coptic-calendar-leap-year-p (year)
  51.   "True if YEAR is a leap year on the Coptic calendar."
  52.   (zerop (mod (1+ year) 4)))
  53.  
  54. (defun coptic-calendar-last-day-of-month (month year)
  55.   "Return last day of MONTH, YEAR on the Coptic calendar.
  56. The 13th month is not really a month, but the 5 (6 in leap years) day period of
  57. Nisi (Kebus)  at the end of the year."
  58.   (if (< month 13)
  59.       30
  60.     (if (coptic-calendar-leap-year-p year)
  61.         6
  62.       5)))
  63.  
  64. (defun calendar-absolute-from-coptic (date)
  65.   "Compute absolute date from Coptic date DATE.
  66. The absolute date is the number of days elapsed since the (imaginary)
  67. Gregorian date Sunday, December 31, 1 BC."
  68.   (let ((month (extract-calendar-month date))
  69.         (day (extract-calendar-day date))
  70.         (year (extract-calendar-year date)))
  71.     (+ (1- coptic-calendar-epoch);; Days before start of calendar
  72.        (* 365 (1- year))         ;; Days in prior years
  73.        (/ year 4)                ;; Leap days in prior years
  74.        (* 30 (1- month))         ;; Days in prior months this year
  75.        day)))                    ;; Days so far this month
  76.        
  77.  
  78. (defun calendar-coptic-from-absolute (date)
  79.   "Compute the Coptic equivalent for absolute date DATE.
  80. The result is a list of the form (MONTH DAY YEAR).
  81. The absolute date is the number of days elapsed since the imaginary
  82. Gregorian date Sunday, December 31, 1 BC."
  83.   (if (< date coptic-calendar-epoch)
  84.       (list 0 0 0);; pre-Coptic date
  85.     (let* ((approx (/ (- date coptic-calendar-epoch)
  86.                       366))   ;; Approximation from below.
  87.            (year              ;; Search forward from the approximation.
  88.             (+ approx
  89.                (calendar-sum y approx
  90.                  (>= date (calendar-absolute-from-coptic (list 1 1 (1+ y))))
  91.                  1)))
  92.            (month             ;; Search forward from Tot.
  93.             (1+ (calendar-sum m 1
  94.                   (> date
  95.                      (calendar-absolute-from-coptic
  96.                       (list m
  97.                             (coptic-calendar-last-day-of-month m year)
  98.                             year)))
  99.                   1)))
  100.            (day                ;; Calculate the day by subtraction.
  101.             (- date
  102.                (1- (calendar-absolute-from-coptic (list month 1 year))))))
  103.     (list month day year))))
  104.  
  105. (defun calendar-coptic-date-string (&optional date)
  106.   "String of Coptic date of Gregorian DATE.
  107. Returns the empty string if DATE is pre-Coptic calendar.
  108. Defaults to today's date if DATE is not given."
  109.   (let* ((coptic-date (calendar-coptic-from-absolute
  110.                        (calendar-absolute-from-gregorian
  111.                         (or date (calendar-current-date)))))
  112.          (y (extract-calendar-year coptic-date))
  113.          (m (extract-calendar-month coptic-date)))
  114.     (if (< y 1)
  115.         ""
  116.       (let ((monthname (aref coptic-calendar-month-name-array (1- m)))
  117.             (day (int-to-string (extract-calendar-day coptic-date)))
  118.             (dayname nil)
  119.             (month (int-to-string m))
  120.             (year (int-to-string y)))
  121.         (mapconcat 'eval calendar-date-display-form "")))))
  122.  
  123. (defun calendar-print-coptic-date ()
  124.   "Show the Coptic calendar equivalent of the selected date."
  125.   (interactive)
  126.   (let ((f (calendar-coptic-date-string (calendar-cursor-to-date t))))
  127.     (if (string-equal f "")
  128.         (message "Date is pre-%s calendar" coptic-name)
  129.       (message f))))
  130.  
  131. (defun calendar-goto-coptic-date (date &optional noecho)
  132.   "Move cursor to Coptic date DATE.
  133. Echo Coptic date unless NOECHO is t."
  134.   (interactive (coptic-prompt-for-date))
  135.   (calendar-goto-date (calendar-gregorian-from-absolute
  136.                        (calendar-absolute-from-coptic date)))
  137.   (or noecho (calendar-print-coptic-date)))
  138.  
  139. (defun coptic-prompt-for-date ()
  140.   "Ask for a Coptic date."
  141.   (let* ((today (calendar-current-date))
  142.          (year (calendar-read
  143.                 (format "%s calendar year (>0): " coptic-name)
  144.                 '(lambda (x) (> x 0))
  145.                 (int-to-string
  146.                  (extract-calendar-year
  147.                   (calendar-coptic-from-absolute
  148.                    (calendar-absolute-from-gregorian today))))))
  149.          (completion-ignore-case t)
  150.          (month (cdr (assoc
  151.                       (capitalize
  152.                        (completing-read
  153.                         (format "%s calendar month name: " coptic-name)
  154.                         (mapcar 'list
  155.                                 (append coptic-calendar-month-name-array nil))
  156.                         nil t))
  157.                       (calendar-make-alist coptic-calendar-month-name-array
  158.                                            1 'capitalize))))
  159.          (last (coptic-calendar-last-day-of-month month year))
  160.          (day (calendar-read
  161.                (format "%s calendar day (1-%d): " coptic-name last)
  162.                '(lambda (x) (and (< 0 x) (<= x last))))))
  163.     (list (list month day year))))
  164.  
  165. (defun diary-coptic-date ()
  166.   "Coptic calendar equivalent of date diary entry."
  167.   (let ((f (calendar-coptic-date-string (calendar-cursor-to-date t))))
  168.     (if (string-equal f "")
  169.         (format "Date is pre-%s calendar" coptic-name)
  170.       f)))
  171.  
  172. (defconst ethiopic-calendar-month-name-array
  173.   ["Maskaram" "Teqemt" "Khedar" "Takhsas" "Ter" "Yakatit" "Magabit" "Miyazya"
  174.    "Genbot" "Sane" "Hamle" "Nahas" "Paguem"])
  175.  
  176. (defconst ethiopic-calendar-epoch 2430
  177.   "Absolute date of start of Ethiopic calendar = August 29, 7 C.E. (Julian).")
  178.  
  179. (defconst ethiopic-name "Ethiopic")
  180.  
  181. (defun calendar-absolute-from-ethiopic (date)
  182.   "Compute absolute date from Ethiopic date DATE.
  183. The absolute date is the number of days elapsed since the (imaginary)
  184. Gregorian date Sunday, December 31, 1 BC."
  185.   (let ((coptic-calendar-epoch ethiopic-calendar-epoch))
  186.     (calendar-absolute-from-coptic date)))
  187.  
  188. (defun calendar-ethiopic-from-absolute (date)
  189.   "Compute the Ethiopic equivalent for absolute date DATE.
  190. The result is a list of the form (MONTH DAY YEAR).
  191. The absolute date is the number of days elapsed since the imaginary
  192. Gregorian date Sunday, December 31, 1 BC."
  193.   (let ((coptic-calendar-epoch ethiopic-